#READ ME--------
#Please note that Policy A = Sanctions
#Please note that Policy B = Bilateral Summit
#Please note that Policy C = In-Theatre Deployment
#Please note that Policy D = Deterrent Deployment
#Please note that Policy E = Multilateral Summit
#Please note that Policy F = Reiterate Guarantee 


#Run all setup code before beginning replication. 
#Setup Code ----------
#Load Libraries
library(xtable)
library(stargazer)
rm(list=ls())

#Load in Data
df_full<-read.csv("/Users/l.sukin/Desktop/EEurope/combined_data.csv")

#Dataset Full Sample
df_sample<-df_full[df_full$Consent==1,] #Remove people who failed the consent.
df_sample<-df_sample[df_sample$State!=99,] #Remove people who did not indicate a state. 

#Dataset Main
df_clean<-df_full[df_full$Consent==1,] #Remove people who failed the consent.
df_clean<-df_clean[df_clean$AttnCheck==1,] #Remove people who failed the attention check.
df_clean<-df_clean[!is.na(df_clean$Vet),] #Remove people who didn't get to the end. 
df_clean<-df_clean[df_clean$ManCheck==1,] #Remove people who failed the manipulation check. 
df_clean<-df_clean[df_clean$Duration..in.seconds.>358,] #Remove speeders

#Dataset Dropping Stateless Respondents
df_clean2<-df_clean
df_clean2<-df_clean2[df_clean$State!=99,] #Remove those who didn't report a state.

#Table 1: % Respondents indicating a U.S. policy would convey... (Main Text) --------------
df<-df_clean

df$Support_Binary<-ifelse(df$Support>2,1,0)
df$Effective_Binary<-ifelse(df$Effective>2,1,0)
df$Economy_Binary<-ifelse(df$Economy>2,1,0)
df$Reputation_Binary<-ifelse(df$Reputation>2,1,0)
df$Interests_Binary<-ifelse(df$Interests>2,1,0)
df$Capabilities_Binary<-ifelse(df$Capabilities>2,1,0)
df$Defend_Binary<-ifelse(df$Defend>2,1,0)

#Percents - Binary
Support_Binary <- c(mean(df$Support_Binary[df$PolicyA==1]),
             mean(df$Support_Binary[df$PolicyB==1]),
             mean(df$Support_Binary[df$PolicyC==1]),
             mean(df$Support_Binary[df$PolicyD==1]),
             mean(df$Support_Binary[df$PolicyE==1]),
             mean(df$Support_Binary[df$PolicyF==1]))

Effective_Binary <- c(mean(df$Effective_Binary[df$PolicyA==1]),
               mean(df$Effective_Binary[df$PolicyB==1]),
               mean(df$Effective_Binary[df$PolicyC==1]),
               mean(df$Effective_Binary[df$PolicyD==1]),
               mean(df$Effective_Binary[df$PolicyE==1]),
               mean(df$Effective_Binary[df$PolicyF==1]))

Economy_Binary <- c(mean(df$Economy_Binary[df$PolicyA==1]),
             mean(df$Economy_Binary[df$PolicyB==1]),
             mean(df$Economy_Binary[df$PolicyC==1]),
             mean(df$Economy_Binary[df$PolicyD==1]),
             mean(df$Economy_Binary[df$PolicyE==1]),
             mean(df$Economy_Binary[df$PolicyF==1]))

Reputation_Binary <- c(mean(df$Reputation_Binary[df$PolicyA==1]),
                mean(df$Reputation_Binary[df$PolicyB==1]),
                mean(df$Reputation_Binary[df$PolicyC==1]),
                mean(df$Reputation_Binary[df$PolicyD==1]),
                mean(df$Reputation_Binary[df$PolicyE==1]),
                mean(df$Reputation_Binary[df$PolicyF==1]))

Interests_Binary <- c(mean(df$Interests_Binary[df$PolicyA==1]),
               mean(df$Interests_Binary[df$PolicyB==1]),
               mean(df$Interests_Binary[df$PolicyC==1]),
               mean(df$Interests_Binary[df$PolicyD==1]),
               mean(df$Interests_Binary[df$PolicyE==1]),
               mean(df$Interests_Binary[df$PolicyF==1]))

Capabilities_Binary <- c(mean(df$Capabilities_Binary[df$PolicyA==1]),
                  mean(df$Capabilities_Binary[df$PolicyB==1]),
                  mean(df$Capabilities_Binary[df$PolicyC==1]),
                  mean(df$Capabilities_Binary[df$PolicyD==1]),
                  mean(df$Capabilities_Binary[df$PolicyE==1]),
                  mean(df$Capabilities_Binary[df$PolicyF==1]))

Defend_Binary <- c(mean(df$Defend_Binary[df$PolicyA==1]),
            mean(df$Defend_Binary[df$PolicyB==1]),
            mean(df$Defend_Binary[df$PolicyC==1]),
            mean(df$Defend_Binary[df$PolicyD==1]),
            mean(df$Defend_Binary[df$PolicyE==1]),
            mean(df$Defend_Binary[df$PolicyF==1]))

table1<-cbind(Reputation_Binary,Interests_Binary,Defend_Binary,Capabilities_Binary)
row.names(table1)<-c("Sanctions","Bilateral Summit","In-Theater Deployment","Deterrent Deployment","Multilateral Summit","Reiterate Guarantee")
table1<-table1*100
xtable(round(table1,2))

#Table 2: How often do respondents prefer each policy over any alternatives? (Main Text) -----------
#IMPORTANT: FULLY RUN THE TABLE 1 CODE BEFORE RUNNING THE CODE BELOW.
table2<-cbind(Support_Binary)
xtable(round(table2,4)*100)
#This code provides the values for "% Approve". 





for(i in 1:nrow(df)){
  df$SupportA[i] <- sum(sum(df$AB[i]==1,na.rm=T), sum(df$AC[i]==1,na.rm=T), sum(df$AD[i]==1,na.rm=T), sum(df$AE[i]==1,na.rm=T), sum(df$AF[i]==1,na.rm=T))
  df$SupportA[i] <- df$SupportA[i] / sum(!is.na(df$AB[i]),!is.na(df$AC[i]),!is.na(df$AD[i]),!is.na(df$AE[i]),!is.na(df$AF[i]))
  
  df$SupportB[i] <- sum(sum(df$AB[i]==2,na.rm=T), sum(df$BC[i]==2,na.rm=T), sum(df$BD[i]==2,na.rm=T), sum(df$BE[i]==2,na.rm=T), sum(df$BF[i]==2,na.rm=T))
  df$SupportB[i] <- df$SupportB[i] / sum(!is.na(df$AB[i]),!is.na(df$BC[i]),!is.na(df$BD[i]),!is.na(df$BE[i]),!is.na(df$BF[i]))
  
  df$SupportC[i] <- sum(sum(df$AC[i]==3,na.rm=T), sum(df$BC[i]==3,na.rm=T), sum(df$CD[i]==3,na.rm=T), sum(df$CE[i]==3,na.rm=T), sum(df$CF[i]==3,na.rm=T))
  df$SupportC[i] <- df$SupportC[i] / sum(!is.na(df$AC[i]),!is.na(df$BC[i]),!is.na(df$CD[i]),!is.na(df$CE[i]),!is.na(df$CF[i]))
  
  df$SupportD[i] <- sum(sum(df$AD[i]==4,na.rm=T), sum(df$BD[i]==4,na.rm=T), sum(df$CD[i]==4,na.rm=T), sum(df$DE[i]==4,na.rm=T), sum(df$DF[i]==4,na.rm=T))
  df$SupportD[i] <- df$SupportD[i] / sum(!is.na(df$AD[i]),!is.na(df$BD[i]),!is.na(df$CD[i]),!is.na(df$DE[i]),!is.na(df$DF[i]))
  
  df$SupportE[i] <- sum(sum(df$AE[i]==5,na.rm=T), sum(df$BE[i]==5,na.rm=T), sum(df$CE[i]==5,na.rm=T), sum(df$DE[i]==5,na.rm=T), sum(df$EF[i]==5,na.rm=T))
  df$SupportE[i] <- df$SupportE[i] / sum(!is.na(df$AE[i]),!is.na(df$BE[i]),!is.na(df$CE[i]),!is.na(df$DE[i]),!is.na(df$EF[i]))
  
  df$SupportF[i] <- sum(sum(df$AF[i]==6,na.rm=T), sum(df$BF[i]==6,na.rm=T), sum(df$CF[i]==6,na.rm=T), sum(df$DF[i]==6,na.rm=T), sum(df$EF[i]==6,na.rm=T))
  df$SupportF[i] <- df$SupportF[i] / sum(!is.na(df$AF[i]),!is.na(df$BF[i]),!is.na(df$CF[i]),!is.na(df$DF[i]),!is.na(df$EF[i]))
}

supports<-c(mean(df$SupportA, na.rm=T),
            mean(df$SupportB, na.rm=T),
            mean(df$SupportC, na.rm=T),
            mean(df$SupportD, na.rm=T),
            mean(df$SupportE, na.rm=T),
            mean(df$SupportF, na.rm=T))
cnames<-c("A (Sanctions)","B (Bilateral Summit)","C (Deploy on Location)","D (Deploy to You)","E (Multilateral Summit)","F (Reiterate Guarantee")
supports<-round(supports,4)*100
xtable(cbind(cnames,supports))
#This provides the values for "% Prefer". 

#Table 3: Regression Results (Main Text)------------
df<-df_clean2

#Variable Setup
df$Direct<-ifelse(df$PolicyB==1 | df$PolicyE==1 | df$PolicyC==1 | df$PolicyD==1,1,0) 
df$Support_Binary<-ifelse(df$Support>2,1,0)
df$Effective_Binary<-ifelse(df$Effective>2,1,0)
df$Economy_Binary<-ifelse(df$Economy>2,1,0)
df$Reputation_Binary<-ifelse(df$Reputation>2,1,0)
df$Interests_Binary<-ifelse(df$Interests>2,1,0)
df$Capabilities_Binary<-ifelse(df$Capabilities>2,1,0)
df$Defend_Binary<-ifelse(df$Defend>2,1,0)
#Please note that the paper refers to "direct" as "high risk." 

#MAIN MODELS
mod1<-(lm(data=df, Support_Binary ~ Direct))
summary(mod1)

mod2<-(lm(data=df, Support_Binary ~ Direct + Target + as.factor(State)))
summary(mod2) #Base: Target = Lithuania

mod3<-(lm(data=df, Support_Binary ~ Direct + Target + as.factor(State) +
            Female + Income + Edu + Age_1 + Rightwing + Vet))
summary(mod3)

mod4<-(lm(data=df, Support_Binary ~ Direct + Target + as.factor(State) +
            Female + Income + Edu + Age_1 + Rightwing + Vet + 
            Know_NatSec + Favor_US + Favor_R + Favor_Uk + Favor_Target + Favor_U + Favor_NATO +
            Dove + Nationalism + ILaw + Revenge))
summary(mod4)

stargazer(mod1,mod2,mod3,mod4, single.row=T)

#Table 4: % respondents indicating policy would adequately address Russian threat (Main Text)------------
df<-df_clean2

df$PolicyTreat<-ifelse(df$PolicyA==1,"A",NA)
df$PolicyTreat<-ifelse(df$PolicyB==1,"B",df$PolicyTreat)
df$PolicyTreat<-ifelse(df$PolicyC==1,"C",df$PolicyTreat)
df$PolicyTreat<-ifelse(df$PolicyD==1,"D",df$PolicyTreat)
df$PolicyTreat<-ifelse(df$PolicyE==1,"E",df$PolicyTreat)
df$PolicyTreat<-ifelse(df$PolicyF==1,"F",df$PolicyTreat)

tab4<-table(df$PolicyTreat,df$Effective)
tab4[1,]<-round(tab4[1,]/sum(tab4[1,])*100,2)
tab4[2,]<-round(tab4[2,]/sum(tab4[2,])*100,2)
tab4[3,]<-round(tab4[3,]/sum(tab4[3,])*100,2)
tab4[4,]<-round(tab4[4,]/sum(tab4[4,])*100,2)
tab4[5,]<-round(tab4[5,]/sum(tab4[5,])*100,2)
tab4[6,]<-round(tab4[6,]/sum(tab4[6,])*100,2)
tab4means<-round(c(mean(df$Effective[df$PolicyTreat=="A"],na.rm=T),
            mean(df$Effective[df$PolicyTreat=="B"],na.rm=T),
            mean(df$Effective[df$PolicyTreat=="C"],na.rm=T),
            mean(df$Effective[df$PolicyTreat=="D"],na.rm=T),
            mean(df$Effective[df$PolicyTreat=="E"],na.rm=T),
            mean(df$Effective[df$PolicyTreat=="F"],na.rm=T)),2)
tab4<-cbind(tab4,tab4means)
tab4

#Table 5: % respondents indicating policy would adequately address Russian threat (Main Text)------------
col1<-c(mean(df$RConT2_NukApprove),
        mean(df$RThreatT2_NukApprove),
        mean(df$RNukT2_NukApprove))

col2<-c(mean(df$RConU2_NukApprove),
       mean(df$RThreatU2_NukApprove),
       mean(df$RNukU2_NukApprove))

tab5<-cbind(col1,col2)
round(tab5*100,0)

#Table 6: Respondents Per State (Appendix)----------
df<-df_sample
xtable(table(df$State))
#Table 7: Balance Table (Appendix)-----------------
df<-df_clean2
library(cobalt)
df$State1<-as.factor(df$State)
df$Male<-ifelse(df$Female==0,1,0)
covs<-subset(df, select=c(State1,Language,Male,Income,Edu,Age_1,Rightwing,Vet))

x<-(bal.tab(covs, treat = df$Short))
xtable(as.data.frame((x[2][1][1])))

#Table 8: Pre-Test Results (Appendix) -------------------------
PolandPretest<-read.csv("~/Dropbox/Eastern Europe Project/Replication Files/PolandPretest.csv")
PolandPretest<-PolandPretest[PolandPretest$Consent==1,]
PolandPretest<-PolandPretest[PolandPretest$Attn==1,]

PolandPretest$PolicyA_Adopt_Binary <- ifelse(PolandPretest$PolicyA_Adopt>2,1,0)
round(mean(PolandPretest$PolicyA_Adopt_Binary,na.rm=T)*100,0)

PolandPretest$PolicyB_Adopt_Binary <- ifelse(PolandPretest$PolicyB_Adopt>2,1,0)
round(mean(PolandPretest$PolicyB_Adopt_Binary,na.rm=T)*100,0)

PolandPretest$PolicyC_Adopt_Binary <- ifelse(PolandPretest$PolicyC_Adopt>2,1,0)
round(mean(PolandPretest$PolicyC_Adopt_Binary,na.rm=T)*100,0)

PolandPretest$PolicyD_Adopt_Binary <- ifelse(PolandPretest$PolicyD_Adopt>2,1,0)
round(mean(PolandPretest$PolicyD_Adopt_Binary,na.rm=T)*100,0)

PolandPretest$PolicyE_Adopt_Binary <- ifelse(PolandPretest$PolicyE_Adopt>2,1,0)
round(mean(PolandPretest$PolicyE_Adopt_Binary,na.rm=T)*100,0)

PolandPretest$PolicyF_Adopt_Binary <- ifelse(PolandPretest$PolicyF_Adopt>2,1,0)
round(mean(PolandPretest$PolicyF_Adopt_Binary,na.rm=T)*100,0)
#Table 9: Poland & Romania Only (Appendix)---------
df<-df_clean[df_clean$State==1 | df_clean$State == 5,]

#Variable Setup
df$Direct<-ifelse(df$PolicyB==1 | df$PolicyE==1 | df$PolicyC==1 | df$PolicyD==1,1,0) 
df$Support_Binary<-ifelse(df$Support>2,1,0)
df$Effective_Binary<-ifelse(df$Effective>2,1,0)
df$Economy_Binary<-ifelse(df$Economy>2,1,0)
df$Reputation_Binary<-ifelse(df$Reputation>2,1,0)
df$Interests_Binary<-ifelse(df$Interests>2,1,0)
df$Capabilities_Binary<-ifelse(df$Capabilities>2,1,0)
df$Defend_Binary<-ifelse(df$Defend>2,1,0)
#Please note that the paper refers to "direct" as "high risk." 

#MAIN MODELS
mod1<-(lm(data=df, Support_Binary ~ Direct))
summary(mod1)

mod2<-(lm(data=df, Support_Binary ~ Direct + Target + as.factor(State)))
summary(mod2) #Base: Target = Lithuania

mod3<-(lm(data=df, Support_Binary ~ Direct + Target + as.factor(State) +
            Female + Income + Edu + Age_1 + Rightwing + Vet))
summary(mod3)

mod4<-(lm(data=df, Support_Binary ~ Direct + Target + as.factor(State) +
            Female + Income + Edu + Age_1 + Rightwing + Vet + 
            Know_NatSec + Favor_US + Favor_R + Favor_Uk + Favor_Target + Favor_U + Favor_NATO +
            Dove + Nationalism + ILaw + Revenge))
summary(mod4)

stargazer(mod1,mod2,mod3,mod4, single.row=T)





#Table 10: Scaled Dependent Variable (Appendix)---------
df<-df_clean2

#Variable Setup
df$Direct<-ifelse(df$PolicyB==1 | df$PolicyE==1 | df$PolicyC==1 | df$PolicyD==1,1,0) 
df$Support_Binary<-ifelse(df$Support>2,1,0)
df$Effective_Binary<-ifelse(df$Effective>2,1,0)
df$Economy_Binary<-ifelse(df$Economy>2,1,0)
df$Reputation_Binary<-ifelse(df$Reputation>2,1,0)
df$Interests_Binary<-ifelse(df$Interests>2,1,0)
df$Capabilities_Binary<-ifelse(df$Capabilities>2,1,0)
df$Defend_Binary<-ifelse(df$Defend>2,1,0)
#Please note that the paper refers to "direct" as "high risk." 

#MAIN MODELS
mod1<-(lm(data=df, Support ~ Direct))
summary(mod1)

mod2<-(lm(data=df, Support ~ Direct + Target + as.factor(State)))
summary(mod2) #Base: Target = Lithuania

mod3<-(lm(data=df, Support ~ Direct + Target + as.factor(State) +
            Female + Income + Edu + Age_1 + Rightwing + Vet))
summary(mod3)

mod4<-(lm(data=df, Support ~ Direct + Target + as.factor(State) +
            Female + Income + Edu + Age_1 + Rightwing + Vet + 
            Know_NatSec + Favor_US + Favor_R + Favor_Uk + Favor_Target + Favor_U + Favor_NATO +
            Dove + Nationalism + ILaw + Revenge))
summary(mod4)

stargazer(mod1,mod2,mod3,mod4, single.row=T)








#Table 11: Robustness Tests (Appendix)---------

#Mod1 only drops respondents that failed attention checks. 
#Mod2 only drops respondents that failed manipulation checks. 
#Mod3 does not drop any respondents.

df<-df_full

#Variable Setup
df$Direct<-ifelse(df$PolicyB==1 | df$PolicyE==1 | df$PolicyC==1 | df$PolicyD==1,1,0) 
df$Support_Binary<-ifelse(df$Support>2,1,0)
df$Effective_Binary<-ifelse(df$Effective>2,1,0)
df$Economy_Binary<-ifelse(df$Economy>2,1,0)
df$Reputation_Binary<-ifelse(df$Reputation>2,1,0)
df$Interests_Binary<-ifelse(df$Interests>2,1,0)
df$Capabilities_Binary<-ifelse(df$Capabilities>2,1,0)
df$Defend_Binary<-ifelse(df$Defend>2,1,0)
#Please note that the paper refers to "direct" as "high risk." 


mod1<-(lm(data=df[df$AttnCheck==1,], Support_Binary ~ Direct + Target + as.factor(State) +
            Female + Income + Edu + Age_1 + Rightwing + Vet + 
            Know_NatSec + Favor_US + Favor_R + Favor_Uk + Favor_Target + Favor_U + Favor_NATO +
            Dove + Nationalism + ILaw + Revenge))
summary(mod1)

mod2<-(lm(data=df[df$ManCheck==1,], Support_Binary ~ Direct + Target + as.factor(State) +
            Female + Income + Edu + Age_1 + Rightwing + Vet + 
            Know_NatSec + Favor_US + Favor_R + Favor_Uk + Favor_Target + Favor_U + Favor_NATO +
            Dove + Nationalism + ILaw + Revenge))
summary(mod2)

mod3<-(lm(data=df, Support_Binary ~ Direct + Target + as.factor(State) +
            Female + Income + Edu + Age_1 + Rightwing + Vet + 
            Know_NatSec + Favor_US + Favor_R + Favor_Uk + Favor_Target + Favor_U + Favor_NATO +
            Dove + Nationalism + ILaw + Revenge))
summary(mod3)

stargazer(mod1,mod2,mod3, single.row=T)

#Table 12: Ranked Order Policy Preferences (Appendix)-----------------
df<-df_clean
preferAover<-c(0,
               sum(df$AB==1,na.rm=T)/sum(table(df$AB)),
               sum(df$AC==1,na.rm=T)/sum(table(df$AC)),
               sum(df$AD==1,na.rm=T)/sum(table(df$AD)),
               sum(df$AE==1,na.rm=T)/sum(table(df$AE)),
               sum(df$AF==1,na.rm=T)/sum(table(df$AF)))*100

preferBover<-c(sum(df$AB==2,na.rm=T)/sum(table(df$AB)),
               0,
               sum(df$BC==2,na.rm=T)/sum(table(df$BC)),
               sum(df$BD==2,na.rm=T)/sum(table(df$BD)),
               sum(df$BE==2,na.rm=T)/sum(table(df$BE)),
               sum(df$BF==2,na.rm=T)/sum(table(df$BF)))*100

preferCover<-c(sum(df$AC==3,na.rm=T)/sum(table(df$AC)),
               sum(df$BC==3,na.rm=T)/sum(table(df$BC)),
               0,
               sum(df$CD==3,na.rm=T)/sum(table(df$CD)),
               sum(df$CE==3,na.rm=T)/sum(table(df$CE)),
               sum(df$CF==3,na.rm=T)/sum(table(df$CF)))*100

preferDover<-c(sum(df$AD==4,na.rm=T)/sum(table(df$AD)),
               sum(df$BD==4,na.rm=T)/sum(table(df$BD)),
               sum(df$CD==4,na.rm=T)/sum(table(df$CD)),
               0,
               sum(df$DE==4,na.rm=T)/sum(table(df$DE)),
               sum(df$DF==4,na.rm=T)/sum(table(df$DF)))*100

preferEover<-c(sum(df$AE==5,na.rm=T)/sum(table(df$AE)),
               sum(df$BE==5,na.rm=T)/sum(table(df$BE)),
               sum(df$CE==5,na.rm=T)/sum(table(df$CE)),
               sum(df$DE==5,na.rm=T)/sum(table(df$DE)),
               0,
               sum(df$EF==5,na.rm=T)/sum(table(df$EF)))*100

preferFover<-c(sum(df$AF==6,na.rm=T)/sum(table(df$AF)),
               sum(df$BF==6,na.rm=T)/sum(table(df$BF)),
               sum(df$CF==6,na.rm=T)/sum(table(df$CF)),
               sum(df$DF==6,na.rm=T)/sum(table(df$DF)),
               sum(df$EF==6,na.rm=T)/sum(table(df$EF)),
               0)*100
tab2<-cbind(preferAover,preferBover,preferCover,preferDover,preferEover,preferFover)
row.names(tab2)<-c("A","B","C","D","E","F")
xtable(tab2)



#Table 13: Interdependence of Capabilities and Interests (Appendix)-----------------
df<-df_clean
mod0<-(lm(data=df, Defend ~ Effective + Economy + Reputation + Interests + Capabilities))
mod1<-(lm(data=df[df$PolicyA==1,], Defend ~  Effective + Economy + Reputation + Interests + Capabilities))
mod2<-(lm(data=df[df$PolicyB==1,], Defend ~  Effective + Economy + Reputation + Interests + Capabilities))
mod3<-(lm(data=df[df$PolicyC==1,], Defend ~  Effective + Economy + Reputation + Interests + Capabilities))
mod4<-(lm(data=df[df$PolicyD==1,], Defend ~  Effective + Economy + Reputation + Interests + Capabilities))
mod5<-(lm(data=df[df$PolicyE==1,], Defend ~  Effective + Economy + Reputation + Interests + Capabilities))
mod6<-(lm(data=df[df$PolicyF==1,], Defend ~  Effective + Economy + Reputation + Interests + Capabilities))
stargazer(mod0,mod1,mod2,mod3,mod4,mod5,mod6,single.row=T)


